const {
  confirm
} = require("../../js/newUtil")

const componentBehavior = require('../../js/common/ComponentBehavior')
const { searchHistory } = require("../../js/modules/store")
import {config} from '../../js/modules/gloab';
const app = getApp()

// components/search-suggestion/search-suggestion.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  observers: {
    showComponent (showComponent) {
      if (!showComponent) {
        // 当关闭搜索时，将展开的内容收起来
        this.setData({
          moreHistory: false, // 显示更多搜索记录
          moreFind: false, // 显示更多搜索发现
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showComponent: false,
    suggestion: {
      history: [],
      find: [],
    },
    moreHistory: false, // 显示更多搜索记录
    moreFind: false, // 显示更多搜索发现
    loaded: false, // 数据是否已加载完成
    defaultShowLength: 10, // 默认显示最大条数，超过则显示下拉按钮
    showHistorySearch: false, // 是否显示搜索历史组件
  },

  lifetimes: {
    attached() {
      var that = this;
      var fkid = app.dns.proprietorId;
      var fkflag = app.dns.proprietor;
      config.getWholesaleConfigs(
      fkid,
      fkflag,
      "Wholesale",
      function(model) {
        console.log(model)
        that.setData({
          showHistorySearch: model.ShowHistorySearch
        })
      });
      this.getData()
    },
  },

  pageLifetimes: {
    hide() {
      this.hide()
    }
  },

  behaviors: [componentBehavior],

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取搜索记录
     */
    getData() {
      let that = this
      // 从服务器获取搜索记录
      searchHistory.getData().then((res) => {
        res = res.Content || {
          historyuser: {},
          historysys: {}
        }
        that.setData({
          suggestion: {
            history: res.historyuser.list || [],
            find: res.historysys.list || []
          },
          loaded: true
        })
      })
    },
    /**
     * 显示组件
     */
    show() {
      // 后台未开启功能时不显示
      if (!this.data.showHistorySearch) {
        return
      }
      this.setData({
        showComponent: true
      })
      this.getData()
    },
    /**
     * 隐藏组件
     */
    hide() {
      this.setData({
        showComponent: false
      })
    },
    /**
     * 删除全部历史记录
     */
    deleteAll: async function () {
      let confirmRes = await confirm({
        content: '请确认是否删除全部历史搜索记录？'
      })
      if (!confirmRes.confirm) {
        return
      }
      // 请求后台删除关键词接口
      this.setData({
        'suggestion.history': []
      })
      searchHistory.remove(true).then((res) => {
        console.log(res)
      })
    },
    /**
     * 点击搜索
     * @param {*} e 
     */
    search(e) {
      let item = e.currentTarget.dataset.item
      this.hide()
      this.triggerEvent('search', {
        keyword: item.SearchKey || ''
      })
    },
    /**
     * 点击搜索
     * @param {*} e 
     */
    addKeyword(keyword) {
      if (keyword.length == 0) {
        return
      }
      let {
        suggestion
      } = this.data
      let list = suggestion.history || []
      let index = list.findIndex(item => item.SearchKey == keyword)
      if (index == -1) {
        list.unshift({
          SearchKey: keyword
        })
      } else {
        list.splice(index, 1)
        list.unshift({
          SearchKey: keyword
        })
      }
      suggestion.history = list
      this.setData({
        suggestion
      })
      // 请求后台添加关键词接口
      console.log('请求后台添加关键词接口')
      searchHistory.add(keyword).then((res) => {
        console.log(res)
      })
    },
    /**
     * 切换显示更多历史记录
     */
    toggleMoreHistory() {
      this.setData({
        moreHistory: !this.data.moreHistory,
        moreFind: false
      })
    },
    /**
     * 切换显示更多推荐
     */
    toggleMoreFind() {
      this.setData({
        moreFind: !this.data.moreFind,
        moreHistory: false
      })
    },
    /**
     * 阻止点击事件穿透
     */
    nothing() {
      return false
    }
  }
})